{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}{% trans 'Email' %}{% endblock %}

{% block breadcrumbs %}
    <ul>
        <li class="firstB"><a href="{{ '/'|alink }}">{% trans 'Home' %}</a></li>
        <li><a href="{{ 'system'|alink }}">{% trans 'Settings' %}</a></li>
        <li class="lastB">{% trans 'Email' %}</li>
    </ul>
{% endblock %}


{% set active_menu = 'system' %}
{% block content %}
<div class="widget simpleTabs">

    <ul class="tabs">
        <li><a href="#tab-index">{% trans 'Email templates' %}</a></li>
        <li><a href="#tab-new">{% trans 'New template' %}</a></li>
        <li><a href="#tab-email">{% trans 'Email settings' %}</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-index">

            {{ mf.table_search }}
            <table class="tableStatic wide">
                <thead>
                    <tr>
                        <td style="width: 50%">{% trans 'Title' %}</td>
                        <td>{% trans 'Extension' %}</td>
                        <td>{% trans 'Code' %}</td>
                        <td>{% trans 'Enabled' %}</td>
                        <td width="13%">&nbsp;</td>
                    </tr>
                </thead>

                <tbody>
                {% set templates = admin.email_template_get_list({"per_page":100, "page":request.page}|merge(request)) %}
                {% for i, template in templates.list %}
                <tr>
                    <td>{{ template.subject }}</td>
                    <td>{{ template.category }}</td>
                    <td>{{ template.action_code }}</td>
                    <td>{{ mf.q(template.enabled) }}</td>
                    <td class="actions">
                        <a class="bb-button btn14" href="{{ '/email/template'|alink }}/{{template.id}}"><img src="images/icons/dark/pencil.png" alt=""></a>
                        <a class="btn14 bb-rm-tr api-link" href="{{ 'api/admin/email/template_delete'|link({'id' : template.id}) }}" data-api-confirm="Are you sure?" data-api-reload="1"><img src="images/icons/dark/trash.png" alt=""></a>
                    </td>
                    
                </tr>

                {% else %}
                <tr>
                    <td colspan="5">
                        {% trans 'The list is empty. Depending on modules enabled email templates will be inserted after first event occurrence.' %}
                    </td>
                </tr>
                {% endfor %}
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="5">
                            <div class="aligncenter">
                                <a href="{{ 'api/admin/email/batch_template_generate'|link}}" title="" class="btn55 mr10 api-link" data-api-reload="1"><img src="images/icons/middlenav/power.png" alt=""><span>Generate templates</span></a>
                                <a href="{{ 'api/admin/email/batch_template_enable'|link}}" title="" class="btn55 mr10 api-link" data-api-msg="All email templates enabled"><img src="images/icons/middlenav/play2.png" alt=""><span>Enable all</span></a>
                                <a href="{{ 'api/admin/email/batch_template_disable'|link}}" title="" class="btn55 mr10 api-link" data-api-msg="All email templates disabled"><img src="images/icons/middlenav/stop.png" alt=""><span>Disable all</span></a>
                            </div>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        
        {% set params = admin.extension_config_get({"ext":"mod_email"}) %}
        <div id="tab-email" class="tab_content nopadding">
            <div class="help">
                <h3>{% trans 'Configure email options' %}</h3>
                <p>{% trans 'BoxBilling sends emails using <em>sendmail</em> by default or you can configure your own SMTP server' %}</p>
            </div>

            <form method="post" action="{{ 'api/admin/extension/config_save'|link }}" class="mainForm api-form" data-api-msg="Email settings updated">
                <fieldset>
                
                <div class="rowElem noborder">
                    <label class="topLabel">Queue options</label>
                    <div class="formBottom" id="mailer">
                        <label>{% trans %}Send emails per cron run (0 = unlimited){% endtrans %}</label><input type="text" name="queue_once" placeholder="0" value="{{ params.queue_once }}"/>
                        <label>{% trans %}Max email sending time in minutes (0 = unlimited, default 5 min.){% endtrans %}</label><input type="text" name="time_limit" placeholder="5" value="{% if params.time_limit %}{{ params.time_limit }}{% else %}5{% endif %}"/>
                        <label>{% trans %}Cancel sending email after unsuccessful tries (0 = do not cancel){% endtrans %}</label><input type="text" name="cancel_after" placeholder="0" value="{{ params.cancel_after }}"/>
                    </div>
                    <div class="fix"></div>
                </div>

                <div class="rowElem noborder">
                    <label class="">{% trans %}Log sent emails to database{% endtrans %}</label>
                    <div class="formRight" id="mailer">
                        <input type="radio" name="log_enabled" value="1" {% if params.log_enabled %}checked="checked"{% endif %}/><label>{% trans 'Yes' %}</label>
                        <input type="radio" name="log_enabled" value="0" {% if not params.log_enabled %}checked="checked"{% endif %} /><label>{% trans 'No' %}</label>
                    </div>
                    <div class="fix"></div>
                </div>

                <div class="rowElem">
                    <label class="">{% trans %}Email transport{% endtrans %}</label>
                    <div class="formRight" id="mailer">
                        <input type="radio" name="mailer" value="sendmail" {% if params.mailer == 'sendmail' or not params.mailer %}checked="checked"{% endif %}/><label>{% trans 'SendMail' %}</label>
                        <input type="radio" name="mailer" value="smtp" {% if params.mailer == 'smtp'%}checked="checked"{% endif %} /><label>{% trans 'SMTP' %}</label>
                    </div>
                    <div class="fix"></div>
                </div>
                </fieldset>

                <fieldset class="smtp" {% if params.mailer != 'smtp'%}style="display:none;"{% endif %} >
                <div class="rowElem">
                    <label class="topLabel">{% trans %}SMTP Hostname{% endtrans %}</label>
                    <div class="formBottom">
                        <input type="text" name="smtp_host" value="{{params.smtp_host}}" placeholder="smtp.gmail.com"/>
                    </div>
                    <div class="fix"></div>
                </div>
                    
                <div class="rowElem">
                    <label class="topLabel">{% trans %}SMTP Port{% endtrans %}</label>
                    <div class="formBottom">
                        <input type="text" name="smtp_port" value="{{params.smtp_port}}" placeholder="587"/>
                    </div>
                    <div class="fix"></div>
                </div>
                    
                <div class="rowElem">
                    <label class="topLabel">{% trans %}SMTP Username{% endtrans %}</label>
                    <div class="formBottom">
                        <input type="text" name="smtp_username" value="{{params.smtp_username}}" placeholder="my.email@gmail.com"/>
                    </div>
                    <div class="fix"></div>
                </div>
                    
                <div class="rowElem">
                    <label class="topLabel">{% trans %}SMTP Password{% endtrans %}</label>
                    <div class="formBottom">
                        <input type="password" name="smtp_password" value="{{params.smtp_password}}" placeholder="******"/>
                    </div>
                    <div class="fix"></div>
                </div>
                    
                <div class="rowElem">
                    <label class="">{% trans %}SMTP Security{% endtrans %}</label>
                    <div class="formRight">
                        <input type="radio" name="smtp_security" value=""{% if not params.smtp_security %}checked="checked"{% endif %}/><label>None</label>
                        <input type="radio" name="smtp_security" value="ssl"{% if params.smtp_security == 'ssl' %}checked="checked"{% endif %}/><label>SSL</label>
                        <input type="radio" name="smtp_security" value="tls"{% if params.smtp_security == 'tls' %}checked="checked"{% endif %}/><label>TLS</label>
                    </div>
                    <div class="fix"></div>
                </div>
                </fieldset>
                    
                <fieldset>
                    <input type="submit" value="{% trans 'Update email settings' %}" class="greyishBtn submitForm" />
                    <input type="button" value="{% trans 'Send test email to staff members' %}" class="blackBtn leftBtn" id="emailTest"/>
                    <input type="hidden" name="ext" value="mod_email"/>
                </fieldset>
            </form>
        </div>

        <div class="tab_content nopadding" id="tab-new">
            <div class="help">
                <h3>{% trans 'Where I can use new email template?' %}</h3>
                <p>{% trans 'Newly created email templates can be used in custom event hooks.' %}</p>
            </div>

            <form method="post" action="{{ 'api/admin/email/template_create'|link }}" class="mainForm save api-form" data-api-reload="1">
                <fieldset>
                    <div class="rowElem noborder">
                        <label>{% trans 'Enabled' %}:</label>
                        <div class="formRight noborder">
                            <input type="radio" name="enabled" value="1"/><label>{% trans 'Yes' %}</label>
                            <input type="radio" name="enabled" value="0" checked="checked"/><label>{% trans 'No' %}</label>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Category' %}</label>
                        <div class="formRight">
                            <input type="text" name="category" value="{{ requests.category }}" required="required" placeholder="General"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Action code' %}</label>
                        <div class="formRight">
                            <input type="text" name="action_code" value="{{ requests.action_code }}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Subject' %}</label>
                        <div class="formRight">
                            <input type="text" name="subject" value="{{ requests.subject }}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>
                    <div class="rowElem">
                        <label>{% trans 'Content' %}</label>
                        <div class="formRight">
                            <textarea name="content" cols="5" rows="10">{{ requests.content }}</textarea>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <input type="submit" value="{% trans 'Create' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block js %}
<script type="text/javascript">
$(function() {
    $('#emailTest').click(function(){
        bb.post('admin/email/send_test', null, function(result){
            bb.msg('Email was successfully sent');
        });
        return false;
    });
	$("#mailer input").click( function() {
        if($(this).val() == 'smtp') {
            $('fieldset.smtp').slideDown();
        } else {
            $('fieldset.smtp').slideUp();
        }
	});
});
</script>
{% endblock %}
